BINOM

Overview

The BINOM function computes values from the binomial distribution, a fundamental discrete probability distribution that models the number of successes in a fixed number of independent Bernoulli trials. Each trial has only two possible outcomes (success or failure) with a constant probability of success. This distribution is widely used in quality control, clinical trials, A/B testing, and any scenario involving binary outcomes.

The binomial distribution is characterized by two parameters: n (the number of trials) and p (the probability of success on each trial). The probability mass function (PMF) gives the probability of observing exactly k successes:

P(X = k) = \binom{n}{k} p^k (1-p)^{n-k}

where \binom{n}{k} = \frac{n!}{k!(n-k)!} is the binomial coefficient, and k \in \{0, 1, 2, \ldots, n\}.

This function supports multiple computation modes:

  • pmf: Probability mass function — probability of exactly k successes
  • cdf: Cumulative distribution function — probability of at most k successes
  • sf: Survival function — probability of more than k successes (equivalent to 1 - \text{CDF})
  • icdf: Inverse CDF (percent point function) — returns the smallest k such that CDF(k) ≥ q
  • isf: Inverse survival function — returns the smallest k such that SF(k) ≤ q
  • mean, var, std, median: Distribution statistics where mean = np and variance = np(1-p)

The optional loc parameter shifts the distribution along the number line, which can be useful for modeling distributions that don’t start at zero.

This implementation uses SciPy’s scipy.stats.binom module, which leverages the Boost Math C++ library for high-performance numerical computations. For related distributions, see the negative binomial and hypergeometric distributions.

This example function is provided as-is without any representation of accuracy.

Excel Usage

=BINOM(k, n, p, binom_mode, loc)
  • k (list[list], required): Value(s) at which to evaluate the distribution. For statistics modes (mean, var, std, median), this is ignored.
  • n (int, required): Number of trials (must be >= 0).
  • p (float, required): Probability of success (0 <= p <= 1).
  • binom_mode (str, optional, default: “pmf”): Output type for the binomial distribution calculation.
  • loc (float, optional, default: 0): Location parameter that shifts the distribution.

Returns (float): Distribution result (float), or error message string.

Examples

Example 1: PMF with required arguments only

Inputs:

k n p
3 10 0.5

Excel formula:

=BINOM(3, 10, 0.5)

Expected output:

Result
0.1172

Example 2: CDF with some optional arguments

Inputs:

k n p binom_mode
3 10 0.5 cdf

Excel formula:

=BINOM(3, 10, 0.5, "cdf")

Expected output:

Result
0.1719

Example 3: Survival function with all arguments

Inputs:

k n p binom_mode loc
3 10 0.5 sf 0

Excel formula:

=BINOM(3, 10, 0.5, "sf", 0)

Expected output:

Result
0.8281

Example 4: Mean of the distribution

Inputs:

k n p binom_mode loc
0 10 0.5 mean 0

Excel formula:

=BINOM(0, 10, 0.5, "mean", 0)

Expected output:

5

Python Code

from scipy.stats import binom as scipy_binom

def binom(k, n, p, binom_mode='pmf', loc=0):
    """
    Compute Binomial distribution values: PMF, CDF, SF, ICDF, ISF, mean, variance, std, or median.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binom.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        k (list[list]): Value(s) at which to evaluate the distribution. For statistics modes (mean, var, std, median), this is ignored.
        n (int): Number of trials (must be >= 0).
        p (float): Probability of success (0 <= p <= 1).
        binom_mode (str, optional): Output type for the binomial distribution calculation. Valid options: pmf, cdf, sf, icdf, isf, mean, var, std, median. Default is 'pmf'.
        loc (float, optional): Location parameter that shifts the distribution. Default is 0.

    Returns:
        float: Distribution result (float), or error message string.
    """
    def to2d(x):
        return [[x]] if not isinstance(x, list) else x

    # Validate n
    try:
        n_val = int(n)
        if n_val < 0:
            return "Invalid input: n must be >= 0."
    except Exception:
        return "Invalid input: n must be an integer."
    # Validate p
    try:
        p_val = float(p)
        if not (0 <= p_val <= 1):
            return "Invalid input: p must be between 0 and 1."
    except Exception:
        return "Invalid input: p must be a number."
    # Validate loc
    try:
        loc_val = float(loc)
    except Exception:
        return "Invalid input: loc must be a number."
    # Validate binom_mode
    valid_modes = {"pmf", "cdf", "sf", "icdf", "isf", "mean", "var", "std", "median"}
    if not isinstance(binom_mode, str) or binom_mode not in valid_modes:
        return f"Invalid input: binom_mode must be one of {sorted(valid_modes)}."

    # Handle statistics
    if binom_mode == "mean":
        return float(scipy_binom.mean(n_val, p_val, loc=loc_val))
    if binom_mode == "var":
        return float(scipy_binom.var(n_val, p_val, loc=loc_val))
    if binom_mode == "std":
        return float(scipy_binom.std(n_val, p_val, loc=loc_val))
    if binom_mode == "median":
        return float(scipy_binom.median(n_val, p_val, loc=loc_val))

    # PMF, CDF, SF, ICDF, ISF
    k_2d = to2d(k)

    def compute(val):
        try:
            kval = float(val)
        except Exception:
            return None

        if binom_mode == "pmf":
            return float(scipy_binom.pmf(kval, n_val, p_val, loc=loc_val))
        elif binom_mode == "cdf":
            return float(scipy_binom.cdf(kval, n_val, p_val, loc=loc_val))
        elif binom_mode == "sf":
            return float(scipy_binom.sf(kval, n_val, p_val, loc=loc_val))
        elif binom_mode == "icdf":
            return float(scipy_binom.ppf(kval, n_val, p_val, loc=loc_val))
        elif binom_mode == "isf":
            return float(scipy_binom.isf(kval, n_val, p_val, loc=loc_val))

    result = []
    for row in k_2d:
        if not isinstance(row, list):
             return "Invalid input: k must be a scalar or 2D list."
        result_row = []
        for val in row:
            out = compute(val)
            if out is None:
                return "Invalid input: k must be a number."
            result_row.append(out)
        result.append(result_row)
    return result

Online Calculator